package edu.androidecs.condescendingquiz;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class QuizActivity extends Activity
{
	TextView quizLevelText;
	TextView categoryText;
	TextView questionText;
	TextView tauntText;
	//ImageView condescendingImageView;
	
	Quiz curQuiz;
	
	Button tauntReturnButton;
	
	public static int curQuizNumber=0;
	
	QuizManager qm;
	public static final int USER_RESP_ARRAY_SIZE=4;
	
	//All of the buttons to be used:
	Button userResponseArray[];

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_quiz);
		
		qm=new QuizManager(this);
		userResponseArray=new Button[4];
		
		tauntReturnButton=(Button) findViewById(R.id.tauntReturnButton);
		tauntReturnButton.setVisibility(View.GONE);
		tauntReturnButton.setClickable(false);
		
		fetchButtons();
		fetchTextBoxes();
		//condescendingImageView=(ImageView) findViewById(R.id.condescending_image_view);
		
		//loadImage();
		
		//Attempt to load all of the quizzes necessary:
		qm.loadAllData();
	}
	
	/*
	public void loadImage()
	{
		AssetManager clappingHandsManager=getAssets();
		
	}*/
	
	@Override
	protected void onResume()
	{
		super.onResume();
		
		qm.curQuizNum=curQuizNumber;
		fetchQuiz();
		curQuiz.curQuestion=0;
		render();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.quiz, menu);
		return true;
	}
	
	/**
	 * This function makes all of the views invisible so that it the user can be insulted.
	 */
	public void blankForInsult()
	{
		int i;
		
		//Blank out all of the text views:
		quizLevelText.setVisibility(View.GONE);
		categoryText.setVisibility(View.GONE);
		questionText.setVisibility(View.GONE);
		tauntText.setVisibility(View.VISIBLE);
		tauntReturnButton.setVisibility(View.VISIBLE);
		tauntReturnButton.setClickable(true);
		
		//Blank all of the buttons:
		for(i=0;i<userResponseArray.length;i++)
		{
			userResponseArray[i].setVisibility(View.GONE);
		}
	}
	
	/**
	 * This function makes all of the views visible again after the user is insulted.
	 */
	public void unBlank()
	{
		int i;
		
		//Blank out all of the text views:
		quizLevelText.setVisibility(View.VISIBLE);
		categoryText.setVisibility(View.VISIBLE);
		questionText.setVisibility(View.VISIBLE);
		tauntText.setVisibility(View.GONE);
		tauntReturnButton.setVisibility(View.GONE);
		tauntReturnButton.setClickable(false);
		//Blank all of the buttons:
		for(i=0;i<userResponseArray.length;i++)
		{
			userResponseArray[i].setVisibility(View.VISIBLE);
		}
	}
	
	/**
	 * This insults the user after he gets a problem wrong. 
	 */
	public void insultUser()
	{
		blankForInsult();
		
		Question q;
		
		if(curQuiz.curQuestion<curQuiz.NUM_OF_QUESTIONS)
		{
			q=curQuiz.questionsList[curQuiz.curQuestion-1];
			tauntText.setText(q.insultText);
		}
		
	}
	
	/**
	 * This function renders everything in the current level. It accepts 
	 */
	public void render()
	{
		int i;
		Question q;
		
		for(i=0;i<curQuiz.questionsList.length;i++)
		{
			Log.i("CondescendingQuiz", "Question: "+curQuiz.questionsList[i]);
		}
		
		if(curQuiz.curQuestion<curQuiz.NUM_OF_QUESTIONS)
		{
			q=curQuiz.questionsList[curQuiz.curQuestion];
			
			questionText.setText(q.questionText);
			categoryText.setText(q.questionCategory);
			quizLevelText.setText("Level "+(q.questionLevel+1));
			
			for(i=0;i<USER_RESP_ARRAY_SIZE;i++)
			{
				if(q!=null)
				{
					userResponseArray[i].setText(q.answerChoiceArray[i]);
				}
			}
			
			curQuiz.curQuestion++;
		}
		
		else if(curQuizNumber==10)
		{
			this.finish();
		}
		
		else
		{
			curQuizNumber++;
			
			fetchQuiz();
		}
	}
	
	public void fetchQuiz()
	{
		Log.i("CondescendingQuiz", "Current quiz number="+curQuizNumber);
		if(curQuizNumber>QuizManager.QUIZNUM-1)
		{
			curQuizNumber=0;
			Intent startFinishIntent=new Intent(this, DoneActivity.class);
			
			startActivity(startFinishIntent);
			
			this.finish();
		}
		
		else
		{
		
			curQuiz=qm.fetchExplicitLevel(curQuizNumber);
			resetQuiz();
		//curQuiz=qm.fetchNextLevel();
		}
		
		
	}
	
	public void fetchTextBoxes()
	{
		quizLevelText=(TextView) findViewById(R.id.leveltext);
		categoryText=(TextView) findViewById(R.id.categorytext);
		questionText=(TextView) findViewById(R.id.questiontext);
		tauntText=(TextView) findViewById(R.id.taunttext);
		tauntText.setVisibility(View.GONE);
	}
	
	/**
	 * Gets all buttons from their respective R values.
	 */
	public void fetchButtons()
	{
		userResponseArray[0]=(Button)findViewById(R.id.response1button);
		userResponseArray[1]=(Button)findViewById(R.id.response2button);
		userResponseArray[2]=(Button)findViewById(R.id.response3button);
		userResponseArray[3]=(Button)findViewById(R.id.response4button);
	}
	
	//Resets the current quiz.
	public void resetQuiz()
	{
		curQuiz.curQuestion=0;
		render();
	}
	
	public void responseOne(View v)
	{
		if(curQuiz.questionsList[curQuiz.curQuestion-1].correctAnswer==0)
		{
			render();
		}
		
		else
		{
			insultUser();
			//new Thread(new RedFlashRunnable()).start();
			//resetQuiz();
		}
	}
	
	public void responseTwo(View v)
	{
		if(curQuiz.questionsList[curQuiz.curQuestion-1].correctAnswer==1)
		{
			//User got it right. Move on to the next one.
			render();
		}
		
		else
		{
			insultUser();
			//new Thread(new RedFlashRunnable()).start();
			//resetQuiz();
		}
	}
	
	public void responseThree(View v)
	{
		if(curQuiz.questionsList[curQuiz.curQuestion-1].correctAnswer==2)
		{
			render();
		}
		
		else
		{
			insultUser();
			//new Thread(new RedFlashRunnable()).start();
			//resetQuiz();
		}
	}
	
	public void responseFour(View v)
	{
		if(curQuiz.questionsList[curQuiz.curQuestion-1].correctAnswer==3)
		{
			render();
		}
		
		else
		{
			insultUser();
			//new Thread(new RedFlashRunnable()).start();
			//resetQuiz();
		}
	}
	
	Context cntxt=this;

	public class RedFlashRunnable implements Runnable
	{

		@Override
		public void run()
		{
		}
		
	}
	
	public void doTauntReturn(View v)
	{
		unBlank();
		resetQuiz();
	}
}
